<template>
  <a-spin :spinning="loading">
    <div>
      <a-form-model
        class="fromModels"
        :model="config"
        ref="differentialLevelProfitModel"
        layout="vertical"
        :labelCol="{ span: 10 }"
        :wrapperCol="{ span: 14 }"
      >
        <div class="centes">
          <div class="centes_confis">
            <a-form-model-item label="级差平级分润开关">
              <a-radio-group :disabled="disableds" v-model="status">
                <a-radio :value="1"> 开 </a-radio>
                <a-radio :value="0"> 关 </a-radio>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="税费费率">
              <a-input :disabled="disableds" v-model="config.tax_rate">
                <div slot="addonAfter">万</div>
              </a-input>
            </a-form-model-item>
          </div>
        </div>

        <div class="confish">
          <div class="confish_items">
            <div class="confish_items_blocks">
              <a-form-model-item label="代还费率级差分润">
                <a-radio-group :disabled="disableds" v-model="config.repayment_rate_differ_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="代还费率平级分润">
                <a-radio-group :disabled="disableds" v-model="config.repayment_rate_force_same_level">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="级差平级同时参与">
                <a-radio-group :disabled="disableds" v-model="config.repayment_rate_differ_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="是否开启同级">
                <a-radio-group :disabled="disableds" v-model="config.repayment_rate_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="平级级别">
                <a-checkbox-group :disabled="disableds" v-model="config.repayment_rate_equal_level">
                  <a-checkbox v-for="(item, index) in levelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-checkbox>
                </a-checkbox-group>
              </a-form-model-item>
              <a-form-model-item label="收益级别">
                <a-select :disabled="disableds" v-model="config.repayment_fee_level_differ_type">
                  <a-select-option v-for="(item, index) in differLevelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item
                v-for="(item, index) in config.repayment_rate_equal_rate"
                :prop="'repayment_rate_equal_rate.' + index"
                :key="'repayment_rate_equal_rate' + index"
                :label="'费率' + index"
                :rules="[{ required: true, message: '请输入费率' + index, trigger: 'blur' }]"
              >
                <div class="disse">
                  <a-input :disabled="disableds" v-model="config.repayment_rate_equal_rate[index]">
                    <div slot="addonAfter">万</div>
                  </a-input>
                  <a-icon
                    class="disseicon"
                    type="minus-circle"
                    @click="lineChanges('repayment_rate_equal_rate', 'del', index)"
                  />
                </div>
              </a-form-model-item>
              <a-form-model-item :wrapperCol="{ span: 8, offset: 15 }">
                <a-button :disabled="disableds" type="primary" @click="lineChanges('repayment_rate_equal_rate', 'add')"
                  >新增一行</a-button
                >
              </a-form-model-item>
            </div>

            <div class="confish_items_blocks">
              <a-form-model-item label="代还笔数费级差分润">
                <a-radio-group :disabled="disableds" v-model="config.repayment_fee_differ_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="代还笔数费平级分润">
                <a-radio-group :disabled="disableds" v-model="config.repayment_fee_force_same_level">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="级差平级同时参与">
                <a-radio-group :disabled="disableds" v-model="config.repayment_fee_differ_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="是否开启同级">
                <a-radio-group :disabled="disableds" v-model="config.repayment_fee_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="平级级别">
                <a-checkbox-group :disabled="disableds" v-model="config.repayment_fee_equal_level">
                  <a-checkbox v-for="(item, index) in levelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-checkbox>
                </a-checkbox-group>
              </a-form-model-item>
              <a-form-model-item label="收益级别">
                <a-select :disabled="disableds" v-model="config.repayment_fee_level_differ_type">
                  <a-select-option v-for="(item, index) in differLevelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item
                v-for="(item, index) in config.repayment_fee_equal_rate"
                :prop="'repayment_fee_equal_rate.' + index"
                :key="'repayment_fee_equal_rate' + index"
                :label="'金额' + index"
                :rules="[{ required: true, message: '请输入金额' + index, trigger: 'blur' }]"
              >
                <div class="disse">
                  <a-input :disabled="disableds" v-model="config.repayment_fee_equal_rate[index]">
                    <div slot="addonAfter">万</div>
                  </a-input>
                  <a-icon
                    class="disseicon"
                    type="minus-circle"
                    @click="lineChanges('repayment_fee_equal_rate', 'del', index)"
                  />
                </div>
              </a-form-model-item>
              <a-form-model-item :wrapperCol="{ span: 8, offset: 15 }">
                <a-button :disabled="disableds" type="primary" @click="lineChanges('repayment_fee_equal_rate', 'add')"
                  >新增一行</a-button
                >
              </a-form-model-item>
            </div>
          </div>

          <div class="confish_items">
            <div class="confish_items_blocks">
              <a-form-model-item label="快捷费率级差分润">
                <a-radio-group :disabled="disableds" v-model="config.collection_rate_differ_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="快捷费率平级分润">
                <a-radio-group :disabled="disableds" v-model="config.collection_rate_force_same_level">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="级差平级同时参与">
                <a-radio-group :disabled="disableds" v-model="config.collection_rate_differ_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="是否开启同级">
                <a-radio-group :disabled="disableds" v-model="config.collection_rate_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="平级级别">
                <a-checkbox-group :disabled="disableds" v-model="config.collection_rate_equal_level">
                  <a-checkbox v-for="(item, index) in levelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-checkbox>
                </a-checkbox-group>
              </a-form-model-item>
              <a-form-model-item label="收益级别">
                <a-select :disabled="disableds" v-model="config.collection_rate_level_differ_type">
                  <a-select-option v-for="(item, index) in differLevelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item
                v-for="(item, index) in config.collection_rate_equal_rate"
                :prop="'collection_rate_equal_rate.' + index"
                :key="'collection_rate_equal_rate' + index"
                :label="'费率' + index"
                :rules="[{ required: true, message: '请输入费率' + index, trigger: 'blur' }]"
              >
                <div class="disse">
                  <a-input :disabled="disableds" v-model="config.collection_rate_equal_rate[index]">
                    <div slot="addonAfter">万</div>
                  </a-input>
                  <a-icon
                    class="disseicon"
                    type="minus-circle"
                    @click="lineChanges('collection_rate_equal_rate', 'del', index)"
                  />
                </div>
              </a-form-model-item>
              <a-form-model-item :wrapperCol="{ span: 8, offset: 15 }">
                <a-button :disabled="disableds" type="primary" @click="lineChanges('collection_rate_equal_rate', 'add')"
                  >新增一行</a-button
                >
              </a-form-model-item>
            </div>

            <div class="confish_items_blocks">
              <a-form-model-item label="快捷笔数费级差分润">
                <a-radio-group :disabled="disableds" v-model="config.collection_fee_differ_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="快捷笔数费平级分润">
                <a-radio-group :disabled="disableds" v-model="config.collection_fee_force_same_level">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="级差平级同时参与">
                <a-radio-group :disabled="disableds" v-model="config.collection_fee_differ_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="是否开启同级">
                <a-radio-group :disabled="disableds" v-model="config.collection_fee_equal_status">
                  <a-radio :value="1"> 开 </a-radio>
                  <a-radio :value="0"> 关 </a-radio>
                </a-radio-group>
              </a-form-model-item>
              <a-form-model-item label="平级级别">
                <a-checkbox-group :disabled="disableds" v-model="config.collection_fee_equal_level">
                  <a-checkbox v-for="(item, index) in levelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-checkbox>
                </a-checkbox-group>
              </a-form-model-item>
              <a-form-model-item label="收益级别">
                <a-select :disabled="disableds" v-model="config.collection_fee_level_differ_type">
                  <a-select-option v-for="(item, index) in differLevelArrs" :key="index" :value="item.id">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
              <a-form-model-item
                v-for="(item, index) in config.collection_fee_equal_rate"
                :prop="'collection_fee_equal_rate.' + index"
                :key="'collection_fee_equal_rate' + index"
                :label="'金额' + index"
                :rules="[{ required: true, message: '请输入费率' + index, trigger: 'blur' }]"
              >
                <div class="disse">
                  <a-input :disabled="disableds" v-model="config.collection_fee_equal_rate[index]">
                    <div slot="addonAfter">万</div>
                  </a-input>
                  <a-icon
                    class="disseicon"
                    type="minus-circle"
                    @click="lineChanges('collection_fee_equal_rate', 'del', index)"
                  />
                </div>
              </a-form-model-item>
              <a-form-model-item :wrapperCol="{ span: 8, offset: 15 }">
                <a-button :disabled="disableds" type="primary" @click="lineChanges('collection_fee_equal_rate', 'add')"
                  >新增一行</a-button
                >
              </a-form-model-item>
            </div>
          </div>
        </div>

        <a-form-model-item :wrapper-col="{ span: 12, offset: 4 }">
          <a-button type="primary" @click="whites">提交</a-button>
          <a-button type="primary" @click="reset">重置</a-button>
          <a-button type="primary" @click="disableds = !disableds">{{ disableds ? '启用编辑' : '关闭编辑' }}</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-spin>
</template>

<script>
import { getRebateApi, setRebateApi, getLevelApi } from '@/api/return.js'
export default {
  name: 'differentialLevelProfit',

  data() {
    return {
      loading: false,
      disableds: true,
      config: {
        collection_fee_differ_equal_status: 0,
        collection_fee_differ_status: 0,
        collection_fee_equal_level: [],
        collection_fee_equal_rate: [],
        collection_fee_equal_status: 0,
        collection_fee_force_same_level: 0,
        collection_rate_differ_equal_status: 0,
        collection_rate_differ_status: 0,
        collection_rate_equal_level: [],
        collection_rate_equal_rate: [],
        collection_rate_equal_status: 0,
        collection_rate_force_same_level: 0,
        collection_rate_level_differ_type: 0,
        repayment_fee_differ_equal_status: 1,
        repayment_fee_differ_status: 1,
        repayment_fee_equal_level: [],
        repayment_fee_equal_rate: [],
        repayment_fee_equal_status: 1,
        repayment_fee_force_same_level: 1,
        repayment_fee_level_differ_type: 1,
        repayment_rate_differ_equal_status: 1,
        repayment_rate_differ_status: 1,
        repayment_rate_equal_level: [],
        repayment_rate_equal_rate: [],
        repayment_rate_equal_status: 1,
        repayment_rate_force_same_level: 1,
        repayment_rate_level_differ_type: 1,
        tax_rate: 1
      },
      status: 1,
      levelArrs: [],
      differLevelArrs: [{ id: 1, name: '大于等于刷卡人' }]
    }
  },
  created() {
    this.getPrivacyAgreementInfo()
    this.getLevelApi()
  },
  methods: {
    reset() {
      this.getPrivacyAgreementInfo()
      this.getLevelApi()
    },
    lineChanges(key, type, ins) {
      if (this.disableds) {
        return
      }
      if (type == 'add') {
        this.config[key].push('')
      }
      if (type == 'del') {
        this.config[key].splice(ins, 1)
      }
    },

    getLevelApi() {
      getLevelApi({
        brand_id: process.env.VUE_APP_BRAND_ID
      }).then(res => {
        this.levelArrs = res.data
      })
    },
    getPrivacyAgreementInfo() {
      this.loading = true
      getRebateApi({
        type: 'rate_fee_differ_equal_rebate',
        brand_id: process.env.VUE_APP_BRAND_ID
      })
        .then(res => {
          console.log(res.data.config.repayment_rate_equal_rate)
          this.config = res.data.config
          this.status = res.data.status
        })
        .finally(() => {
          this.loading = false
        })
    },
    whites() {
      this.$refs.differentialLevelProfitModel.validate(valid => {
        if (valid) {
          this.setDatas()
          this.getPrivacyAgreementInfo()
          this.getLevelApi()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    setDatas() {
      setRebateApi({
        type: 'rate_fee_differ_equal_rebate',
        brand_id: process.env.VUE_APP_BRAND_ID,
        status: this.status,
        data: this.config
      }).then(res => {
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.centes {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
}
.centes_confis {
  width: 50%;
}
.confish {
  display: flex;
  align-items: self-start;
  justify-content: space-between;
}
.confish_items {
  width: 48%;
}
.confish_items_blocks {
  margin-bottom: 20px;
}
</style>
